/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.CacheUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreSelfProductCategory;
import com.chlitina.store.modules.base.service.StoreSelfProductCategoryService;
import com.chlitina.store.modules.expert.utils.ExpertUtils;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * 店面自营产品类别Controller
 * @author liujiang
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/storeSelfProductCategory")
public class StoreSelfProductCategoryController extends BaseController {

	@Autowired
	private StoreSelfProductCategoryService storeSelfProductCategoryService;
	
	@ModelAttribute
	public StoreSelfProductCategory get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeSelfProductCategoryService.get(id);
		}else{
			return new StoreSelfProductCategory();
		}
	}
	
	private String getSelectPage(Page page){
		Long select_selfProductCategory_id = (Long) CacheUtils.get("select_selfProductCategory_id");
		String currentId = "";
		if(select_selfProductCategory_id != null) {
	        int index = storeSelfProductCategoryService.getIndexById(select_selfProductCategory_id);
	        ExpertUtils.setPageNoByCurrentId(page, index);
	        currentId = String.valueOf(select_selfProductCategory_id);
	        CacheUtils.remove("select_selfProductCategory_id");
		}
		return currentId;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(StoreSelfProductCategory storeSelfProductCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		String searchName = request.getParameter("searchName");
		Page<StoreSelfProductCategory> page = new Page<StoreSelfProductCategory>(request, response);
		String currentId = getSelectPage(page);
        Page<StoreSelfProductCategory> current_page = storeSelfProductCategoryService.find(page, searchName); 
        model.addAttribute("searchName", searchName);
        model.addAttribute("currentId", currentId);
        model.addAttribute("page", current_page);
		return "modules/base/storeSelfProductCategoryList";
	}

	@RequestMapping(value = "form")
	public String form(StoreSelfProductCategory storeSelfProductCategory, Model model) {
		CacheUtils.put("select_selfProductCategory_id", storeSelfProductCategory.getId());
		model.addAttribute("storeSelfProductCategory", storeSelfProductCategory);
		return "modules/base/storeSelfProductCategoryForm";
	}

	@RequestMapping(value = "save")
	public String save(StoreSelfProductCategory storeSelfProductCategory, Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		storeSelfProductCategory.setStore(store);
		if (!beanValidator(model, storeSelfProductCategory)){
			return form(storeSelfProductCategory, model);
		}
		storeSelfProductCategoryService.save(storeSelfProductCategory);
		addMessage(redirectAttributes, "保存店面自营产品类别'" + storeSelfProductCategory.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeSelfProductCategory/";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		storeSelfProductCategoryService.delete(id);
		addMessage(redirectAttributes, "删除店面自营产品类别成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeSelfProductCategory/";
	}

	@ResponseBody
	@RequestMapping(value = "storeProductCategoryData")
	public String storeProductCategoryData(HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		Set<Map<String, Object>> set = Sets.newHashSet();
		List<StoreSelfProductCategory> categoryList = storeSelfProductCategoryService.findAll();
		for (StoreSelfProductCategory category : categoryList) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("name", category.getName());
			map.put("id", category.getId());
			set.add(map);
		}
		return JsonMapper.getInstance().toJson(set);
	}
}
